<?php
class article extends top {

    function __construct(){  
        parent::__construct();
        // if(!$this->check_session())
            // $this->__error("danger","未登录!"); 
    }
    //贴吧
    function article_list(){
        $data=$this->spArgs();
        $m_article = spClass('m_article');
        $conditions = "select * from article order by add_time desc";
        $article = $m_article->findSql($conditions);
        foreach ($article as $key => $val) {
            $arr[]= array(
                'article_id'   => $val['id'],
                'article_title' => $val['title'],
                'article_content'  => $val['content'],
                'add_time'  => $val['add_time'],
                'user_id' => $val['user_id'],
                'user_name' => $val['user_name']
            );
        }

        echo json_encode($arr);
    }
    function single_article(){
        $data=$this->spArgs();
        $m_user = spClass('m_user');
        $m_article = spClass('m_article');

        
        $own_arr = array();
        $condition=array('user_id'=>$data['id']);
        $article_own = $m_article->findAll($condition);
        foreach ($article_own as $key => $val) {
            $own_arr[]= array(
                'article_id'   => $val['id'],
                'article_title' => $val['title'],
                'article_content'  => $val['content'],
                'add_time'  => $val['add_time'],
                'user_id' => $val['user_id'],
                'user_name' => $val['user_name']
            );
        }
        echo json_encode($own_arr);
    }

    //创建文章
    function add_article(){
        $data=$this->spArgs();

        $m_article = spClass('m_article');

        if(!empty($data["title"])){
            $insert = array(
                'title' => $data['title'],
                'content' => $data['content'],
                'user_id' => $_SESSION['id'],
                'user_name' => $_SESSION['username'],
                'add_time'=>date('Y-m-d H:i:s')                
            );
            $a = $m_article->create($insert);
            if($a){
                echo 1;
            }          
        }else{
            $this->__error("danger","发布失败!");
        }

    }
    //删除贴吧
    function delete_article(){
        $data=$this->spArgs();
        $m_article = spClass('m_article');
        $m_article_member = spClass('m_article_member');

        $article_conditions=array('id'=>$data['article_id']);
        $m_article->delete($article_conditions);

        $article_member_condition = array('article_id' => $data['article_id']);
        $a = $m_article_member->delete($article_member_condition);
        if($a){
            echo json_encode("1");
        }else{
            $this->__error("danger","删除失败!");
        }
    }
    //贴吧成员
    function article_member(){
        $data=$this->spArgs();
        $m_user = spClass('m_user');
        $m_article_member = spClass('m_article_member');
        $condition = array('article_id'=>$data['article_id']);
        $article_member = $m_article_member->findAll($condition,'','user_id');

        $user_arr = array();
        foreach ($article_member as $key => $val) {
            $u_condition = array('id' => $val['user_id']);
            $user_info = $m_user->find($u_condition);

            $user_arr[]=array('user_id' => $val['user_id'],'studentid' => $user_info['studentid'],'username' => $user_info['username'],);           
        }
        echo json_encode($user_arr);
    }
    //添加贴吧成员
    function add_member(){
        $data=$this->spArgs();
        //$data['article_id'] = 2;
        if(empty($data['studentids']) || !isset($data['article_id']) || !is_numeric($data['article_id']))
            $this->__error("danger","参数错误!");
        
        $studentids = explode(';', $data['studentids']);

        $m_user = spClass('m_user');
        $m_article_member = spClass('m_article_member');
        $m_article = spClass('m_article');

        foreach ($studentids as $key => $value) {
            $student_info = $m_user->find(array('studentid'=>strtoupper($value)));
            if(empty($student_info)){
                /*
                $to_save_id = $m_user->create(array('studentid'=>$value));
                $insert_data[$to_save_id] = array(
                    'article_id'=> $data['article_id'],
                    'user_id'=>$to_save_id,
                );
                */
                $return['none'][]=array(
                    'studentid' => $value
                );
            }else{
                $article_member_info = $m_article_member->find(array('user_id'=>$student_info['id'],'article_id'=>$data['article_id']));
                $article_info = $m_article->find(array('leader_id'=>$student_info['id'],'id'=>$data['article_id']));

                //判断贴吧是否有这个成员
                if(empty($article_member_info) && empty($article_info)){
                    $insert_data[$student_info['id']] = array(
                        'article_id'=> $data['article_id'],
                        'user_id'=>$student_info['id'],
                    );
                    $return['success'][]=array(
                        'studentid' => $value,
                        'username'  => $student_info['username'],
                        'user_id'   => $student_info['id'],
                    );
                }else{
                    $return['exist'][]=array(
                        'studentid' => $value
                    );
                }
            }
        }
        //TODO 判断是否已在关系表中
        
        if(!empty($insert_data)){           
            $m_article_member->createAll($insert_data);              
        }
        echo json_encode($return);
    }
    //删除贴吧成员
    function delete_member(){
        $data=$this->spArgs();
        //$data['user_id']=36;
        $m_article_member = spClass('m_article_member');
        $conditions=array('user_id'=>$data['user_id']);
        $a=$m_article_member->delete($conditions);
        if($a){
            echo json_encode("1");
        }else{
            $this->__error("danger","删除失败!");
        }
       
    }
}
?>